package com.xy.netty.dome.life.cycle.bound.handler.in;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 1
 */
@Slf4j
public class InboundHandlerTwo extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf data = (ByteBuf) msg;
        log.info("InboundHandlerTwo channelRead 服务端收到数据：{}",data.toString(CharsetUtil.UTF_8));
        //ctx.fireChannelRead(Unpooled.copiedBuffer("InboundHandlerTwo "+data.toString(CharsetUtil.UTF_8), CharsetUtil.UTF_8));
        ctx.channel().writeAndFlush(Unpooled.copiedBuffer(data.toString(CharsetUtil.UTF_8), CharsetUtil.UTF_8));
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("InboundHandlerTwo->exceptionCaught",cause);
        ctx.close();
    }
}
